
//
//  NewsScrollViewCell.m
//  HenanOA
//
//  Created by 党阳阳 on 14-5-21.
//  Copyright (c) 2014年 broadwave.com. All rights reserved.
//

#import "NewsScrollViewCell.h"
#import  "AFNetworking.h"
#import "UIImageView+AFNetworking.h"

@interface NewsScrollViewCell()
@property (nonatomic,strong) UIImageView *imageView;
@property (nonatomic,strong) UILabel *lable;

@end

@implementation NewsScrollViewCell
@synthesize newfd = _newfd;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        
       
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame imageString:(NSString *)imageString titleString:(NSString *)titleString withnewf:(NSString *)nefff
{
    self = [super initWithFrame:frame];
    if (self) {
        _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        if (imageString) {
            
           // [_imageView setImageWithURL:[NSURL URLWithString:imageString]
                                //            placeholderImage:[UIImage imageNamed:@"base_detail_default_pic.png"]];
           
            NSLog(@"开始下图片 %@",imageString);
            NSString *cachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Private Documents/Cache"];
            NSLog(@"cachePath =%@",cachePath);
            NSFileManager *fileManager=[NSFileManager defaultManager];
           
            
            if(![fileManager fileExistsAtPath:cachePath])
            {    NSLog(@"开始创建cache 文件夹");
                [fileManager createDirectoryAtPath:cachePath withIntermediateDirectories:YES attributes:nil error:nil];
            }
            NSLog(@"检查图片是否存在");
            NSString *imagePath = [cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",nefff]];
            NSLog(@"imagePath = %@",imagePath);
            BOOL fileExisted = [fileManager fileExistsAtPath:imagePath];
            
            if (fileExisted) {
                NSLog(@"开始读取缓存图片。。。");
                
                 NSData *imageData = [NSData dataWithContentsOfFile :imagePath];
               // [_imageView setImageWithURL:[NSURL fileURLWithPath:imagePath]
                //             placeholderImage:[UIImage imageNamed:@"base_detail_default_pic.png"]];
                _imageView.image = [UIImage imageWithContentsOfFile:imagePath];
                NSLog(@"读取缓存图片成功");
                
            
            }
            else
            {
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^void (void) {
                  
                    NSData* data1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageString]];
                    if ([data1 length] > 0)
                    {
                        NSError* fff;
                        _imageView.image = [UIImage imageWithData:data1];
                        // NSString *savedImagePath1 = [cachePath stringByAppendingPathComponent:@""];
                        BOOL saveImage  =  [UIImagePNGRepresentation(_imageView.image) writeToFile:imagePath options:NSAtomicWrite error:&fff];
                        //BOOL saveImage = [data1 writeToFile:imagePath atomically:YES];
                        if (saveImage)
                        {
                            NSLog(@"图片缓存成功");
                        }
                        else
                        {
                            NSLog(@"图片缓存失败");
                            NSLog(@"error = %@",fff);
                        }
                        
                    }
                    
                });
              /*  NSData* data1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageString]];
                if ([data1 length] > 0)
                {
                    NSError* fff;
                    _imageView.image = [UIImage imageWithData:data1];
                    // NSString *savedImagePath1 = [cachePath stringByAppendingPathComponent:@""];
                    BOOL saveImage  =  [UIImagePNGRepresentation(_imageView.image) writeToFile:imagePath options:NSAtomicWrite error:&fff];
                    //BOOL saveImage = [data1 writeToFile:imagePath atomically:YES];
                    if (saveImage)
                    {
                        NSLog(@"图片缓存成功");
                    }
                    else
                    {
                        NSLog(@"图片缓存失败");
                        NSLog(@"error = %@",fff);
                    }
                    
                }*/

               /* NSURLRequest* ref = [NSURLRequest requestWithURL:[NSURL URLWithString:imageString]];
                
                [_imageView setImageWithURLRequest:ref placeholderImage:_imageView.image success:^(NSURLRequest* reuq,NSHTTPURLResponse* respone ,UIImage* image)
                 {
                     
                     NSError* fff;
                  
                    
                     BOOL saveImage  =  [UIImagePNGRepresentation(image) writeToFile:imagePath options:NSAtomicWrite error:&fff];
                     if (saveImage)
                     {
                         NSLog(@"图片缓存成功");
                     }
                     else
                     {
                         NSLog(@"图片缓存失败");
                     }
                     
                     
                 }  failure:^(NSURLRequest* reuq,NSHTTPURLResponse* respone ,NSError* error)
                 {
                     
                 }];*/

            

             }
            
            _lable = [[UILabel alloc]initWithFrame:CGRectMake(0, self.frame.size.height-20, 320, 20)];
            _lable.textColor = [UIColor whiteColor];
            _lable.font = [UIFont systemFontOfSize:15];
                      
        }else{
            
            _lable = [[UILabel alloc] initWithFrame:frame];
        
        
        }
        
        _lable.backgroundColor = [UIColor colorWithRed:0.0/255 green:0.0/255 blue:0.0/255 alpha:0.6];
        _lable.text = titleString;
        _lable.textColor = [UIColor whiteColor];
        [self addSubview:_imageView];
        [self addSubview:_lable];
    }
    
    return self;
}

-(void)setImageVie:(UIImage*)f
{
    _imageView.image = f;
}
#pragma -mark private method


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
